package controller;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class loginServlet
 */

@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    @EJB
    Biz.userBeanLocal _userBeanLocal;
    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session=request.getSession();
		response.setContentType("text/html;charset=utf-8");
		_userBeanLocal.login(request.getParameter("user_ID"), request.getParameter("password"));
		request.setAttribute("_userBeanLocal", _userBeanLocal);
		String dest="/login.jsp";
		if(_userBeanLocal.getUser()==null)
		{
			request.setAttribute("message", "login failed");
		}
		else
		{
			session.setAttribute("_userBeanLocal", _userBeanLocal);
			session.setAttribute("user", _userBeanLocal.getUser());
			switch(_userBeanLocal.getUser().getType())
			{
				case "ADMIN":
					dest="/adminPage.jsp";
					break;
				case "COLLECTOR":
					dest="/inputTubeID.jsp";
					break;
				case "ENTRY":
					dest="/updateResult.jsp";
					break;
				case "TESTED":
					dest="/TestedPage.jsp";
					break;
				default:
					break;
			}

		}
		response.sendRedirect("/PCR_System"+dest);

		}

}
